package com.chainyoung.component_tmperson.ui.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.common.view.BaseDialog;
import com.chainyoung.component_tmperson.R;

public class InputDialog {


    private Context mContext;
    private BaseDialog mDialog;


    private String title, remindInfo;
    private int inputType;
    private EditText etContent;

    private onConfimrListener onConfimrListener;


    public InputDialog(Context context, String title, String remindInfo, int inputType) {
        mContext = context;
        this.title = title;
        this.remindInfo = remindInfo;
        this.inputType = inputType;
        init();

    }


    private void init() {
        BaseDialog.Builder mBuilder = new BaseDialog.Builder(mContext);
        mDialog = mBuilder.setViewId(R.layout.layout_input_dialog)
                //.setAnimation(com.jingdu.gamewallet.commonlibs.R.style.Scale_aniamtion)
                .setGravity(Gravity.CENTER)
                .isOnTouchCanceled(false)
                .isCanceled(false)
                .setHeightdp(170)
                .setWidthdp(300)
                .builder();

        TextView tvTitle = mDialog.getView(R.id.tv_title);
        TextView tvRemind = mDialog.getView(R.id.tv_remind);

        tvTitle.setText(title);
        tvRemind.setVisibility(TextUtils.isEmpty(remindInfo) ? View.GONE : View.VISIBLE);
        tvRemind.setText(remindInfo);
        etContent = mDialog.getView(R.id.et_content);
        etContent.setInputType(inputType);
        final TextView tvConfirm = mDialog.getView(R.id.tv_confirm);
        TextView tvCancel = mDialog.getView(R.id.tv_cancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.close();
            }
        });

        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtil.viewGetTextIsEmpty(etContent)) {
                    ToastUtil.showToast(title);
                    return;
                }
                if (onConfimrListener != null) {
                    onConfimrListener.onConfirmListener(StringUtil.viewGetText(etContent));
                }
            }
        });

        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvConfirm.setTextColor(!StringUtil.viewGetTextIsEmpty(etContent) ? ContextCompat.getColor(mContext, R.color._0078f5) : ContextCompat.getColor(mContext, R.color._7e8da6));
            }
        });

    }

    public void setEtContent(String content) {
        etContent.setText(content);
        etContent.setSelection(content.length());
    }

    public void setMaxEditLength(int num){
        InputFilter[] filters = {new InputFilter.LengthFilter(num)};
        etContent.setFilters(filters);
    }

    public void show() {

        mDialog.show();

    }

    public void close() {
        mDialog.close();
    }

    public void setOnConfimrListener(InputDialog.onConfimrListener onConfimrListener) {
        this.onConfimrListener = onConfimrListener;
    }


    public interface onConfimrListener {

        void onConfirmListener(String content);

    }


}
